﻿using AutoMapper;
using B.S.BaseData.Domain.Entities;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interfaces;
using B.S.BaseData.Read.API.Command.RoleCommand;
using MediatR;

namespace B.S.BaseData.Read.API.CommandHandler.RoleHandler
{
    public class OnenessRoleNameCommandHandler : IRequestHandler<OnenessRoleNameCommand, APIResult<object>>
    {
        private readonly IBaseRepository<Role> userrReppos;
        private readonly IMapper mapper;

        public OnenessRoleNameCommandHandler(IBaseRepository<Role> userrReppos, IMapper mapper)
        {
            this.userrReppos = userrReppos;
            this.mapper = mapper;
        }

        public Task<APIResult<object>> Handle(OnenessRoleNameCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var user = userrReppos.GetAll().Where(x => x.RoleName == request.RoleName).ToList();
            if (user.Count() > 0)
            {
                result.Code = APIEnum.Failed;
                result.Msg = "用户名重复";
            }
            else
            {
                result.Code = APIEnum.Success;
                result.Msg = "用户名唯一通过";
            }
            return Task.FromResult(result);
        }
    }
}
